package edu.cmu.cs211.compression.tests;

import java.io.*;
import java.util.Random;

import org.junit.Test;

import edu.cmu.cs211.compression.huffman.HuffmanCompressor;
import edu.cmu.cs211.compression.io.InputStreamBitReader;
import edu.cmu.cs211.compression.io.OutputStreamBitWriter;
import edu.cmu.cs211.compression.lzw.LempelZivCompressor;

public class LZWTest
{
  @Test
  public void testRoundTrip() throws Exception
  {
    byte[] x = new byte[1000];
    new Random(42).nextBytes(x);
    TestUtil.checkRoundTrip(new LempelZivCompressor(), x);
  }

  @Test
  public void simple() throws Exception
  {
    String[] tests = new String[]
    { "", "asdfaaaaaaaadaaadaaaaaaafaaaaaaaaa",
        "The red rear rikes to grrrrrrrrrrr" };
    for (int i = 0; i < tests.length; i++)
    {
      byte[] input = tests[i].getBytes("ASCII");
      TestUtil.checkRoundTrip(new LempelZivCompressor(), input);
    }
  }
  
  /*
  @Test
  public void CheckEncode() throws Exception
  {
    String[] tests = new String[]
    { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "asdfaaaaaaaadaaadaaaaaaafaaaaaaaaa",
        "The red rear rikes to grrrrrrrrrrr" };
   
      byte[] input = tests[0].getBytes("ASCII");
     byte[] output = TestUtil.checkEncode(new LempelZivCompressor(), input);
     
   
     FileInputStream file = new FileInputStream("C:\\Users\\Administrator\\Desktop\\New folder\\constitution.txt");
     InputStreamBitReader reader = new InputStreamBitReader(file);
 	
     File f=new File("C:\\Users\\Administrator\\Desktop\\New folder\\constitution1.lzw");
     FileOutputStream fop=new FileOutputStream(f);
     
     OutputStreamBitWriter out = new OutputStreamBitWriter(fop);
	
     new LempelZivCompressor().compress(reader, out);
     
  }
  */
  @Test
  public void streeTest() throws Exception
  {
	  for(int i = 0; i < 100; i++)
	  {
		// generate a random string and convert it to hex
  		byte [] input = (Long.toHexString(Double.doubleToLongBits(Math.random()))).getBytes("ASCII");
  		TestUtil.checkRoundTrip(new LempelZivCompressor(), input);
	  }
  }
  
  @Test
  public void simpleEdgeCase() throws Exception
  {
    String single = "a";
    byte[] input = single.getBytes("ASCII");
    TestUtil.checkRoundTrip(new LempelZivCompressor(), input);
  }
}
